<link rel="stylesheet" href="__STATIC__/theme/default/css/goods.css?ver={:date('ymd')}">
<div class="fixed-bar">
    <div class="item-title">
        <ul class="tab-base nc-row">
            <li><a href="javascript:void(0);" data-index='1' class="tab current"><span>通用信息</span></a></li>
            <li><a href="javascript:void(0);" data-index='2' class="tab"><span>商品价格</span></a></li>
            <li><a href="javascript:void(0);" data-index='3' class="tab"><span>商品相册</span></a></li>
            <li><a href="javascript:void(0);" data-index='4' class="tab"><span>模型属性</span></a></li>
        </ul>
    </div>
</div>
<form class="layui-form layui-box" style='padding:25px 30px 20px 0' action='{:url("$classuri/action_goods_operation")}' data-auto="true" method="post">
    <!-- 商品通用信息 -->
    <div class="tab_div_1">
        <div class="layui-form-item">
            <label class="layui-form-label">商品模型</label>
            <div class="layui-input-block">
                <select name='model_id' id="model_id" required="required" class='layui-select full-width' style='display:block'>
                    <option value="">请选择</option>
                    {foreach $menus as $menu}
                    <option value='{$menu.id}' {eq name="$vo.model_id|default=''" value="$menu.id" } selected {/eq}>{$menu.spl}{$menu.cname}</option>
                    {/foreach}
                </select>
            </div>
        </div> 
        <div class="layui-form-item">
            <label class="layui-form-label">商品分类</label>
            <div class="layui-input-block">
                <select name='cid' required="required" class='layui-select full-width' style='display:block'>
                    {foreach $menus as $menu}
                    <option value='{$menu.id}'>{$menu.spl}{$menu.cname}</option>
                    {/foreach}
                </select>
            </div>
        </div> 
        <div class="layui-form-item">
            <label class="layui-form-label">商家</label>
            <div class="layui-input-block">
                <select name='shop_id' required="required" class='layui-select full-width' style='display:block'>
                    {foreach $shop_sellers as $menu}
                    <option value='{$menu.id}'>{$menu.sname}</option>
                    {/foreach}
                </select>
            </div>
        </div> 
        <div class="layui-form-item">
            <label class="layui-form-label">商品类型</label>
            <div class="layui-input-block">
                 <label class="think-radio">
                    <input type="radio" name="type" value="1" title="跨境" lay-ignore="" style="display: inline-block;" {eq name="$vo.type|default='1'" value="1" } checked {/eq}>
                    跨境
                </label>
                <label class="think-radio">
                    <input type="radio" name="type" value="2" title="完税" {eq name="$vo.type|default='1'" value="2" } checked {/eq}  lay-ignore="" style="display: inline-block;">
                    完税
                </label>
                <label class="think-radio">
                    <input type="radio" name="type" value="3" title="免税" {eq name="$vo.type|default='1'" value="3" } checked {/eq} lay-ignore="" style="display: inline-block;">
                    免税
                </label>
            </div>
        </div> 
        
        <div class="layui-form-item">
            <label class="layui-form-label">商品名称</label>
            <div class="layui-input-block">
                {if $vo and isset($vo.title)}
                <input type="text" name="title" value='{$vo.title|default=""}'
                       required="required" title="请输入商品名称" placeholder="请输入商品名称" class="layui-input">
                {else}
                <input type="text" name="title" value='{$vo.title|default=""}' required="required" title="请输入商品名称" placeholder="请输入商品名称" class="layui-input">
                {/if}
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">商品别名</label>
            <div class="layui-input-block">
                {if $vo and isset($vo.alias)}
                <input type="text" name="alias" value='{$vo.alias|default=""}'
                       title="请输入商品别名" placeholder="请输入商品别名" class="layui-input">
                {else}
                <input type="text" name="alias" value='{$vo.alias|default=""}' title="请输入商品别名" placeholder="请输入商品别名" class="layui-input">
                {/if}
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">批发价</label>
            <div class="layui-input-block">
                <input type="text" name="wholesaleprice" value='{$vo.wholesaleprice|default="0"}'  required="required" pattern="^\d+(\.\d+)?$"
                       title="请输入批发价" placeholder="请输入批发价" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">市场价</label>
            <div class="layui-input-block">
                <input type="text" name="storeprice" value='{$vo.storeprice|default="0"}'  required="required" pattern="^\d+(\.\d+)?$"
                       title="请输入市场价" placeholder="请输入市场价" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">SKU号</label>
            <div class="layui-input-block">
                <input type="text" name="sku" value='{$vo.sku|default=""}'  placeholder="请输入SKU号" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">重量</label>
            <div class="layui-input-block">
                <input type="text" name="weight" value='{$vo.weight|default="0"}'  required="required" title="请输入商品重量" placeholder="请输入商品重量" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">库存</label>
            <div class="layui-input-block">
                <input type="text" name="stock" value='{$vo.stock|default="0"}'  required="required" title="请输入商品库存" placeholder="请输入商品库存" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">排序</label>
            <div class="layui-input-block">
                <input type="text" name="sort" value='{$vo.sort|default="0"}'  required="required" maxlength="3" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">是否上架</label>
            <div class="layui-input-block">
               <label class="think-radio">
                    <input type="radio" name="status" value="0" title="下架" {eq name="$vo.status|default='1'" value="0" } checked {/eq} lay-ignore="" style="display: inline-block;">
                    下架
                </label>
                <label class="think-radio">
                    <input type="radio" name="status" value="1" title="上架" {eq name="$vo.status|default='1'" value="1" } checked {/eq} lay-ignore="" style="display: inline-block;">
                    上架
                </label>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">是否免运费</label>
            <div class="layui-input-block"> 
               <label class="think-radio">
                    <input type="radio" name="is_free" value="0" title="否" {eq name="$vo.is_free|default='0'" value="0" } checked {/eq} lay-ignore="" style="display: inline-block;">
                    否
                </label>
                <label class="think-radio">
                    <input type="radio" name="is_free" value="1" title="是" {eq name="$vo.is_free|default='0'" value="1" } checked {/eq} lay-ignore="" style="display: inline-block;">
                    是
                </label>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">库存状态</label>
            <div class="layui-input-block"> 
               <label class="think-radio">
                    <input type="radio" name="stock_status" value="0" title="拍下减库存" {eq name="$vo.stock_status|default='0'" value="0" } checked {/eq} lay-ignore="" style="display: inline-block;">
                    拍下减库存
                </label>
                <label class="think-radio">
                    <input type="radio" name="stock_status" value="1" title="支付完成减库存" {eq name="$vo.stock_status|default='0'" value="1" } checked {/eq} lay-ignore="" style="display: inline-block;">
                    支付完成减库存
                </label>
                <label class="think-radio">
                    <input type="radio" name="stock_status" value="2" title="永远不减" {eq name="$vo.stock_status|default='0'" value="2" } checked {/eq} lay-ignore="" style="display: inline-block;">
                    永远不减
                </label>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">是否预售</label>
            <div class="layui-input-block">
               <label class="think-radio">
                    <input type="radio" name="is_presale" value="0" title="否" {eq name="$vo.is_presale|default='0'" value="0" } checked {/eq} lay-ignore="" style="display: inline-block;">
                    否
                </label>
                <label class="think-radio">
                    <input type="radio" name="is_presale" value="1" title="是" {eq name="$vo.is_presale|default='0'" value="1" } checked {/eq} lay-ignore="" style="display: inline-block;">
                    是
                </label>
            </div>
        </div>
        <div class="layui-form-item presale" {eq name="$vo.is_presale|default='0'" value="1" } style="display: block;" {else \} style="display: none;" {/eq}>
            <label class="layui-form-label">预售说明</label>
            <div class="layui-input-block">
                <input type="text" name="presale" value='{$vo.presale|default=""}'  placeholder="请输入预售说明" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">商品描述</label>
            <div class="layui-input-block">
                <textarea name="content">{$vo.content|default=""}</textarea>
            </div>
        </div>
    </div>
    <!-- 商品通用信息 END -->
    
    <!-- 商品价格-->
    <div class="tab_div_2" style="display:none;">
        <div class="layui-form-item">
            <label class="layui-form-label">购买方式</label>
            <div class="layui-input-block">
               <label class="think-radio">
                    <input type="radio" name="buy_status" value="0" title="一口价" {eq name="$vo.buy_status|default='0'" value="0" } checked {/eq} lay-ignore="" style="display: inline-block;">
                    一口价
                </label>
                <label class="think-radio">
                    <input type="radio" name="buy_status" value="1" title="区间价格" {eq name="$vo.buy_status|default='0'" value="1" } checked {/eq} lay-ignore="" style="display: inline-block;">
                    区间价格
                </label>
                <label class="think-radio">
                    <input type="radio" name="buy_status" value="2" title="规格价格" {eq name="$vo.buy_status|default='0'" value="2" } checked {/eq} lay-ignore="" style="display: inline-block;">
                    规格价格
                </label>
            </div>
        </div>
        <!-- 一口价 -->
        <div class="layui-form-item marketprice" {eq name="$vo.buy_status|default='0'" value="0" } style="display: block;"{else \} style="display: none;" {/eq}>
            <label class="layui-form-label">一口价</label>
            <div class="layui-input-block">
                <input type="text" name="marketprice" value='{$vo.marketprice|default="0.00"}' required="required" pattern="^\d+(\.\d+)?$" title="请输入零售价" placeholder="请输入零售价" class="layui-input">
            </div>
        </div>
        <!-- END 一口价 -->

        <!-- 区间价格 -->
        <div class="layui-form-item section_price" {eq name="$vo.buy_status|default='0'" value="1" } style="display: block;"{else \} style="display: none;" {/eq}>
            <table class="layui-table" lay-skin="line" lay-size="sm" style="margin: 10px;">
                <thead>
                    <tr>
                        <th class='text-left nowrap'>数量</th>
                        <th class='text-left nowrap'>单位</th>
                        <th class='text-left nowrap'>价格</th>
                        <th class='text-left nowrap'>操作</th>
                    </tr>
                </thead>
                <tbody class="section">
                    {eq name="$vo.buy_status|default='1'" value="0" }
                        {foreach $section_price as $sp}
                            <tr>
                                <td class='text-left nowrap'>
                                    <input type='text' name='number[{$sp.id}][]' required='required' title='请输入数量'  placeholder='请输入数量' class='layui-input' value="{$sp.number}">
                                </td>
                                <td class='text-left nowrap'>
                                    <select name='unit_id[{$sp.id}][]' required='required' class='layui-select full-width' style='display:block'>
                                        {foreach $goods_unit as $unit}
                                            <option value='{$unit["id"]}' {eq name="$unit.id" value="$sq.unit_id" } selected {/eq}>{$unit["unit"]}</option>;
                                        {/foreach}
                                    </select>
                                </td>
                                <td class='text-left nowrap'>
                                    <input type='text' name='price[{$sp.id}][]' required='required' title='请输入价格' placeholder='请输入价格' class='layui-input' value="{$sp.price}">
                                </td>
                                <td class='text-left nowrap'>
                                    <span class='layui-btn del_section' data-id='{$sp.id}'>删除</span>
                                </td>
                            </tr>;
                        {/foreach}
                    {/eq}
                </tbody>
            </table>
                <span class='layui-btn add_section' style="float: right;">添加价格区间</span>
        </div>
        <!-- END 区间价格 -->

        <!-- 规格价格 -->
        <div class="layui-form-item spec" {eq name="$vo.buy_status|default='0'" value="2" } style="display: block;"{else \} style="display: none;" {/eq}>
            <table class="layui-table" lay-skin="line" lay-size="sm" style="margin: 10px;">
                <thead class="spec_head">
                    {neq name="$spec_price|default=''" value=""}
                    {$spec_price['spec_head']}
                    {/neq}
                </thead>
                <tbody class="sepc_content">
                    {neq name="$spec_price|default=''" value=""}
                    {$spec_price['spec_content']}
                    {/neq}
                </tbody>
            </table>
                <span class='layui-btn add_spec' style="float: right;">添加规格价格</span>
        </div>
        <!-- END 规格价格 -->

    </div>
    <!-- 商品价格 END -->

     <!-- 商品图片-->
    <div class="tab_div_3" style="display:none;">
        <div class="layui-form-item layui-block">
            <label class="layui-form-label">封面图片</label>
            <div style="position:relative">
                {eq name="$cover|default=''" value=""}
                <div class="uploadimage" style="background-image: url('__SRC__{$vo.iname|default='static/theme/default/img/image.png'}');" data-tips-image="__SRC__{$vo.iname|default='static/theme/default/img/image.png'}">
                    <input type="hidden" name="iname">
                </div>
                {else \}
                <div class="uploadimage" style="background-image: url('__SRC__{$cover|default='static/theme/default/img/image.png'}');" data-tips-image="__SRC__{$cover|default='static/theme/default/img/image.png'}">
                    <input type="hidden" name="iname" value="{$cover}">
                </div>
                {/eq}
                <a data-file="one" data-field="iname" data-type="png,jpg" data-uptype="local" href="javascript:void(0)" class="uploadbutton">上传图片</a>
            </div>
        </div>
        <div class="layui-form-item layui-block">
            <label class="layui-form-label">商品图片</label>
            <div style="position:relative" id="file_images">
                {eq name="$goods_images|default=''" value=""}
                     <input type="hidden" name="inames">
                {else \}
                    {foreach $goods_images as $v}
                    <div class="uploadimage transition uploadimagemtl" data-id="{$v.id}" data-tips-image="{$v.iname}" style="background-image: url(&quot;{$v.iname}&quot;);">
                        <a href="javascript:void(0)" class="layui-icon upload-option-button"><span class="fa fa-close"></span></a>
                    </div>
                    {/foreach}
                    <input type="hidden" class="file_images" name="inames" data-id="{$file_id}" data-srcs="{$file_images}" value="{$file_images}">
                {/eq}
                <a data-file="mut" data-field="inames" data-type="png,jpg" data-uptype="local" href="javascript:void(0)">上传图片</a>
            </div>
        </div>
    </div>
    <!-- 商品图片 END -->

    <!-- 模型属性 -->
    <div class="tab_div_4" style="display: none;">
        {neq name="$attr|default=''" value=""}
        {foreach $attr as $a}
            {if condition="$a['type'] eq 1"}
                <div class="layui-form-item">
                    <label class="layui-form-label">{$a['a_name']}</label>
                    <div class="layui-input-block">
                        <input type="text" name="attr_{$vo.model_id}[]" placeholder="请输入{$a['a_name']}" {if condition="$goods_attr neq ''"}{foreach $goods_attr as $v} {if condition="$a['id'] eq $v['attr_id']"} value="{$v['value']}" {/if}{/foreach}{/if} class="layui-input">
                    </div>
                </div>
            {elseif condition="$a['type'] eq 2"/}
                <div class="layui-form-item">
                    <label class="layui-form-label">{$a['a_name']}</label>
                    <div class="layui-input-block">
                        <select name="attr_{$vo.model_id}[]" class="layui-select full-width" style="display:block">
                            <option value="">请选择</option>
                            {foreach $a['a_value'] as $b}
                            <option value="{$b}" {if condition="$goods_attr neq ''"}{foreach $goods_attr as $v} {if condition="$b eq $v['value']"} selected {/if}{/foreach}{/if}>{$b}</option>
                            {/foreach}
                        </select>
                    </div>
                </div>
            {else \}
                <div class="layui-form-item">
                    <label class="layui-form-label">{$a['a_name']}</label>
                    <div class="layui-input-block">
                        <textarea placeholder="请输入{$a['a_name']}" class="layui-textarea" name="attr_{$vo.model_id}[]">{if condition="$goods_attr neq ''"}{foreach $goods_attr as $v} {if condition="$a['id'] eq $v['attr_id']"}{$v['value']}{/if}{/foreach}{/if}</textarea>
                    </div>
                </div>
            {/if}
        {/foreach}
        {/neq}
    </div>
    <!-- END 模型属性 -->
    <div class="hr-line-dashed"></div>

    <div class="layui-form-item text-center">
        {if isset($vo['id'])}<input type='hidden' value='{$vo.id}' name='id'/>{/if}
        <button class="layui-btn" type='submit'>保存数据</button>
        <button class="layui-btn layui-btn-danger" type='button' data-confirm="确定要取消编辑吗？" data-close>取消编辑</button>
    </div>

</form>
<script type="text/javascript">
    // 商品详情
    require(['ckeditor'], function () {
        var editor = window.createEditor('[name="content"]');
    });

    /**
     * 处理规格字符串
     * @param  {[type]} id      [规格属性ID]
     * @param  {[type]} old_id  [原本规格属性ID]
     * @param  {[type]} path    [规格路径]
     * @return {[type]}         [description]
     */
    // function handle_spec(id,old_id,path){
    //     if(old_id == ''){
    //         return path+'_'+id;
    //     }
    //     var sear = new RegExp('_'); 
    //     var path_array = path.split('_');
    //     var new_path = '';
    //     var url = "{:url('admin/goods/ajax_spec_attr_sort')}";
    //     for (var i = path_array.length - 1; i >= 0; i--) {
    //         if(id == ''){
    //             if(path_array[i] != old_id){
    //                 new_path += path_array[i]+'_';
    //             }
    //         }else{
    //             if(path_array[i] == old_id){
    //                 new_path += id+'_';
    //             }else{
    //                 new_path += path_array[i]+'_';
    //             }
    //         }
    //     }
    //     if(sear.test(new_path) !== false){
    //         $.get(url,{'path':new_path},function(data){
    //             if(data.code == 200){
    //                 var spec_path = data.data;
    //             }else{
    //                 alert('系统错误，请联系管理员！');
    //             }
    //         },'json');
    //         return spec_path;
    //     }else{
    //         var spec_path = new_path.substring(0,new_path.length-1);
    //         return spec_path;
    //     }
    // }

    // 规格选择处理
    $('body').on('change','.sel-spec',function(){
        var id = $(this).val();
        var old_id = $(this).attr('old-id');
        var parent = $(this).parent().parent();
        var path = parent.find('.del_spec').attr('data-path');
        var new_path = '';
        
        if(path !== ''){
            // new_path = handle_spec(id,old_id,path);
            if(old_id == ''){
                return path+'_'+id;
            }
            var sear = new RegExp('_'); 
            var path_array = path.split('_');
            var url = "{:url('admin/goods/ajax_spec_attr_sort')}";
            for (var i = path_array.length - 1; i >= 0; i--) {
                if(id == ''){
                    if(path_array[i] != old_id){
                        new_path += path_array[i]+'_';
                    }
                }else{
                    if(path_array[i] == old_id){
                        new_path += id+'_';
                    }else{
                        new_path += path_array[i]+'_';
                    }
                }
            }
            if(sear.test(new_path) !== false){
                $.get(url,{'path':new_path},function(data){
                    if(data.code == 200){
                        var spec_path = data.data;
                        parent.find('.del_spec').attr('data-path',spec_path);
                        parent.find('.spec_price').attr('name','spec_price['+spec_path+'][]');
                        parent.find('.spec_stock').attr('name','spec_stock['+spec_path+'][]');
                        parent.find('.spec_sku').attr('name','spec_sku['+spec_path+'][]');
                    }else{
                        alert('系统错误，请联系管理员！');
                    }
                },'json');
            }else{
                var spec_path = new_path.substring(0,new_path.length-1);
                parent.find('.del_spec').attr('data-path',spec_path);
                parent.find('.spec_price').attr('name','spec_price['+spec_path+'][]');
                parent.find('.spec_stock').attr('name','spec_stock['+spec_path+'][]');
                parent.find('.spec_sku').attr('name','spec_sku['+spec_path+'][]');
            }
        }else{
            spec_path = id;
            parent.find('.del_spec').attr('data-path',spec_path);
            parent.find('.spec_price').attr('name','spec_price['+spec_path+'][]');
            parent.find('.spec_stock').attr('name','spec_stock['+spec_path+'][]');
            parent.find('.spec_sku').attr('name','spec_sku['+spec_path+'][]');
        }
        $(this).attr('old-id',id);
        
    });

    // 添加价格规格
    $('.add_spec').click(function(){
        var model_id = $('#model_id').val();
        var url = '{:url("admin/goods/ajax_get_specattr")}';
        if(model_id == ''){
            alert('请选择商品模型');
            return false;
        }
        $.get(url,{'id':model_id},function(data){
            var attr_str = '';
            var spec_str = '<tr>';
            if(data.code == 200){
                for (var i in data.data) {
                    var str = '<option value="">请选择</option>';
                    if(data.data[i] !== ''){
                        for (var j in data.data[i]) {
                            str += "<option value='"+data.data[i][j]['id']+"'>"+data.data[i][j]['spec_attr']+"</option>";
                        }
                    }
                    spec_str += "<td class='text-left nowrap'>"
                                   +"<select class='layui-select full-width sel-spec' old-id='' style='display:block'>"
                                       +str
                                    +"</select>"
                               +"</td>";
                }
            }

            spec_str += "<td class='text-left nowrap'>"
                            +"<input type='text' name='spec_price[][]' value='' required='required'  placeholder='请输入价格' class='layui-input spec_price'>"
                       +"</td>"
                       +"<td class='text-left nowrap'>"
                           +"<input type='text' name='spec_stock[][]' value='' required='required' placeholder='请输入库存' class='layui-input spec_stock'>"
                       +"</td>"
                       +"<td class='text-left nowrap'>"
                           +"<input type='text' name='spec_sku[][]' value='' placeholder='请输入SKU' class='layui-input spec_sku'>"
                       +"</td>"
                       +"<td class='text-left nowrap'>"
                           +"<span class='layui-btn del_spec' data-path='' data-id=''>删除</span>"
                       +"</td>"
                        +"</tr>";
            $('.sepc_content').append(spec_str);
        },'json');
    });

    // 删除规格价格
    $('body').on('click','.del_spec',function(){
        var id = $(this).attr('data-id');
        if(id !== ''){
            var url = "{:url('admin/goods/ajax_del_spec')}";
            $.get(url,{'id':id},function(data){
                if(data.code == 400){
                    alert('删除失败，请联系管理员！');
                    return false;
                }
            },'json');
        }

        $(this).parent().parent().remove();
    });

    // 模型选择处理
    $('#model_id').change(function(){
        var val = $(this).val();
        if(val == ''){
            // 初始化数据
            $('.tab_div_4').html("");
            $('.spec_head').html("");
            return false;
        }
        var url = '{:url("admin/goods/ajax_get_goodsmodel")}';
        $.get(url,{'model_id':val},function(data){
            var attr_str = '';
            var spec_str = '';
            if(data.code == 200){
                // 商品属性
                // 初始化数据
                var attr = data.data.attr;
                if(attr !== ''){
                    for (var i in attr) {
                        if(attr[i]['type'] == 1){
                            attr_str += '<div class="layui-form-item">'
                                            +'<label class="layui-form-label">'+attr[i]['a_name']+'</label>'
                                            +'<div class="layui-input-block">'
                                                +'<input type="text" name="attr_'+val+'[]" placeholder="请输入'+attr[i]['a_name']+'" class="layui-input">'
                                            +'</div>'
                                        +'</div>';
                        }else if(attr[i]['type'] == 2){
                            var opt_str = '<option value="">请选择</option>';
                            if(attr[i]['a_value'] !== ''){
                                for (var j = attr[i]['a_value'].length - 1; j >= 0; j--) {
                                    opt_str += '<option value="'+attr[i]['a_value'][j]+'">'+attr[i]['a_value'][j]+'</option>'; 
                                }
                            }
                            attr_str += '<div class="layui-form-item">'
                                            +'<label class="layui-form-label">'+attr[i]['a_name']+'</label>'
                                            +'<div class="layui-input-block">'
                                                +'<select name="attr_'+val+'[]" class="layui-select full-width" style="display:block">'
                                                    +opt_str
                                                +'</select>'
                                            +'</div>'
                                        +'</div>';
                        }else if(attr[i]['type'] == 3){
                            attr_str += '<div class="layui-form-item">'
                                            +'<label class="layui-form-label">'+attr[i]['a_name']+'</label>'
                                            +'<div class="layui-input-block">'
                                                +'<textarea placeholder="请输入'+attr[i]['a_name']+'" class="layui-textarea" name="attr_'+val+'[]">'+attr[i]['a_name']+'</textarea>'
                                            +'</div>'
                                        +'</div>';
                        }
                    }
                }

                // 商品规格
                var spec = data.data.spec;
                if(spec !== ''){
                    for (var i in spec) {
                        spec_str += '<th class="text-left nowrap">'+spec[i]['spec_name']+'</th>';
                    }
                }
            }
            var sp = '<tr>'
                    +spec_str
                    +'<th class="text-left nowrap">价格</th>'
                    +'<th class="text-left nowrap">库存</th>'
                    +'<th class="text-left nowrap">SKU</th>'
                    +'<th class="text-left nowrap">操作</th>'
                +'</tr>';
            // 填入数据
            $('.tab_div_4').html(attr_str);
            $('.spec_head').html(sp);
        },'json');
    });


    // 价格方式
    $('input[name="buy_status"]').click(function(){
        var val = $(this).val();
        if(val == 0){
            $('.marketprice').css('display','block');
            $('.section_price').css('display','none');
            $('.spec').css('display','none');
        }else if(val == 1){
            $('.marketprice').css('display','none');
            $('.section_price').css('display','block');
            $('.spec').css('display','none');
        }else if(val == 2){
            $('.marketprice').css('display','none');
            $('.section_price').css('display','none');
            $('.spec').css('display','block');
        }
    });

    // 是否预售
    $('input[name="is_presale"]').click(function(){
        var val = $(this).val();
        if(val == 1){
            $('.presale').css('display','block');
        }else{
            $('.presale').css('display','none');
        }
    });

    // 商品价格区间
    $('.add_section').click(function(){
        var url = '{:url("$classuri/ajax_goods_unit")}';
        $.get(url,'',function(data){
            var str = "<tr>"
                        +"<td class='text-left nowrap'>"
                        +"<input type='text' name='number[][]' value='' required='required' title='请输入数量'  placeholder='请输入数量' class='layui-input'>"
                        +"</td>"
                        +"<td class='text-left nowrap'>"
                        +"<select name='unit_id[][]' required='required' class='layui-select full-width' style='display:block'>";

            if(data.code == 200){
                for (var i = data.data.length - 1; i >= 0; i--) {
                    str += "<option value='"+data.data[i]['id']+"'>"+data.data[i]['unit']+"</option>";
                }
            }                   
            str  += "</select>"
                    +"</td>"
                    +"<td class='text-left nowrap'>"
                    +"<input type='text' name='price[][]' value='' required='required' title='请输入价格' placeholder='请输入价格' class='layui-input'>"
                    +"</td>"
                    +"<td class='text-left nowrap'>"
                    +"<span class='layui-btn del_section' data-id=''>删除</span>"
                    +"</td>"
               +"</tr>";
            $('.section').append(str);
           
        },'json');
    
    });

    // 删除价格区间
    $('body').on("click",".del_section",function(){
        var id = $(this).attr('data-id');
        var url = '{:url("admin/goods/ajax_del_section")}';
        var my = $(this);
        if(id !== ''){
            $.get(url,{'id':id},function(data){
                if(data.code == 200){
                    my.parent().parent().remove();
                }else{
                    alert('删除失败！');
                }
            });
        }else{
            $(this).parent().parent().remove();
        }
    });
  

    $(document).ready(function(){    
        //插件切换列表
        $('.tab-base').find('.tab').click(function(){
            $('.tab-base').find('.tab').each(function(){
                $(this).removeClass('current');
            });
            $(this).addClass('current');
            var tab_index = $(this).data('index');          
            $(".tab_div_1, .tab_div_2, .tab_div_3,.tab_div_4").hide();          
            $(".tab_div_"+tab_index).show();
        });     
            
    });

    // 当有文件上传时，会触发 change 事件
    require(['jquery'], function () {
        $('[name="iname"]').on('change', function () {
            $(this).parent().attr('data-tips-image', this.value).css('backgroundImage', 'url(' + this.value + ')');
        });
    });

    /**
     * 移除相册图片
     */
    $('#file_images').on('click','.upload-option-button',function(e){
        var my = $(this);
        e.stopPropagation();
        $.msg.confirm('确定要移除这张图片吗？', function () {
            var $cur = $('.file_images');
            var images = $cur.data('srcs');
            var id = my.parent().attr('data-id');
            var iname = my.parent().attr('data-tips-image');
            if(id !== ''){
                var url = "{:url('admin/goods/ajax_goods_image')}";
                $.get(url,{'id':id},function(data){
                    if(data.code == 400){
                        alert('删除失败，请联系管理员！');
                        return false;
                    }else{
                        var get_data_id = $('[name="inames"]').attr('data-id').split('|');
                        var data_id = '';
                        for (var i in get_data_id) {
                            data_id += get_data_id[i]+'|';
                        }
                        data_id = data_id.substring(0,data_id.length-1);
                        $('[name="inames"]').attr('data-id',data_id);
                    }
                },'json');
            }
            var srcs = images.split(iname+'|').join('');
            $cur.attr('data-srcs',srcs);
            $cur.val(srcs);
            my.parent().remove();
        });
    });

    require(['jquery'], function () {
        var tpl = '<div class="uploadimage transition uploadimagemtl" data-id=""><a href="javascript:void(0)" class="layui-icon upload-option-button"><span class="fa fa-close"></span></a></div>';
        $('[name="inames"]').on('change', function () {
            var input = this, values = [], val_id = [], srcs = this.value.split('|');
            $(this).prevAll('.uploadimage').map(function () {
                values.push($(this).attr('data-tips-image'));
                val_id.push($(this).attr('data-id'));
            }), $(this).prevAll('.uploadimage').remove();
            values.reverse();
            for (var i in srcs) {
                values.push(srcs[i]);
            }
            this.value = values.join('|');
            for (var i in values) {
                var $tpl = $(tpl).attr('data-tips-image', values[i]).attr('data-id',val_id[i]).css('backgroundImage', 'url(' + values[i] + ')');
                $tpl.data('input', input).data('srcs', values).data('index', i);
                // $tpl.on('click', 'a', function (e) {
                //     e.stopPropagation();
                //     var $cur = $(this).parent();
                //     $.msg.confirm('确定要移除这张图片吗？', function () {
                //         var data = $cur.data('srcs');
                //         delete data[$cur.data('index')];
                //         $cur.data('input').value = data.join('|');
                //         $cur.remove();
                //     });
                // });
                $(this).before($tpl);
            }
        });
    });

  
</script>
